<?php

namespace app\admin\controller;
use app\common\controller\Backend;
use think\DB;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class  Configs extends Backend
{
    /*分红*/
    public function index()
    {
        $bound=DB::name('configs')->paginate(10);
        if($bound)
        {
            if($bound->toArray())
            {
                foreach($bound as $k=>$v)
                {
                    $data=array();
                    $data=$v;
                    if($v['state']==1)
                    {
                        $data['state_name']='启用';
                    }else{
                        $data['state_name']='禁用';
                    }
                    $bound->offsetSet($k,$data);
                }
            }
        }
        $this->assign('page',$bound->render());
        $this->assign('configs',$bound);
        return $this->fetch("configs/index");
    }
    public function add()
    {
        if($_POST)
        {
            $res=$_POST;
            $res['time']=time();
            $state=DB::name('configs')->insert($res);
            if($state)
            {
                return $this->success('操作成功','configs/index');
            }else{
                return $this->error('操作失败');
            }
        }else{
            return $this->fetch('configs/add');
        }
    }
    public function dateil()
    {
        if($_POST)
        {
            $date['name']=$_POST['name'];
            $date['type']=$_POST['type'];
            $date['plotting']=$_POST['plotting'];
            $date['time']=time();
            $date['state']=$_POST['state'];
            $where['id']=$_POST['id'];
            $bounds=DB::name('configs')->where($where)->update($date);
            if($bounds)
            {
                return $this->success('操作成功','configs/index');
            }else{
                return $this->error('操作失败');
            }
        }else{
            $id=$this->request->param('id');
            $where['id']=$id;
            $bounds=DB::name('configs')->where($where)->find();
            $this->assign('configs',$bounds);
            return $this->fetch('configs/dateil');
        }
    }
    /*删除配置*/
    public function delete()
    {
        $id=$this->request->param('id');
        $where['id']=$id;
        $state=DB::name('configs')->where($where)->delete();
        if($state)
        {
            return $this->success('操作成功！');
        }else{
            return $this->error('操作失败!');
        }
    }
}
